import java.awt.color.ICC_ColorSpace;

public class MySingleList {
    public class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //定义表头
    public ListNode head;
    //创建链表
    public void createList(){
        ListNode node1=new ListNode(12);
        ListNode node2=new ListNode(26);
        ListNode node3=new ListNode(30);
        ListNode node4=new ListNode(45);
        ListNode node5=new ListNode(56);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        this.head=node1;
    }
    //打印链表
    public void show(){
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    //链表长度
    public int size(){
        ListNode cur=head;
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    //看方法中是否包含key
    public boolean contains(int key){
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
        ListNode node=new ListNode(data);
        if(head==null){
            head=node;
            return;
        }
        node.next=head;
        head=node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node=new ListNode(data);
        if(head==null){
            head=node;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        //cur.next=null
        cur.next=node;
    }
    //插入链表
    public void addIndex(int index,int val){
        if(index<0||index>size()){
            return;
        }
        if(index==0){
            addFirst(val);
        }
        else if(index==size()){
            addLast(val);
        }else{
            ListNode node=new ListNode(val);
            ListNode cur=findPindex(index);
            node.next=cur.next;
            cur.next=node;
        }
    }
    //找出index前一个节点
    private ListNode findPindex(int index){
        int i=1;
        ListNode cur=head;
        while(i<=index-1){
            cur=cur.next;
            i++;
        }
        return cur;
    }
    //删除第一个出现的关键字key
    public void remove(int key){
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
        }
        ListNode cur=findPkey(key);
        if(cur==null){
            return;
        }
        ListNode dve=cur.next;
        cur.next=dve.next;
    }
    //找出key前一个节点
    private ListNode findPkey(int key){
        ListNode cur=head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
    //删除链表中的所有key元素
    public void removeAllKey(int key){
        if(head==null){
            return;
        }
        ListNode cur=head.next;
        ListNode prev=head;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }
    public void clear(){
        ListNode cur=head;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=null;
            cur=curN;
        }
        head=null;
    }
    //反转链表
    public ListNode reverseList(ListNode head){
        if(head==null){
            return null;
        }
        ListNode cur=head.next;
        head.next=null;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=head;
            head=cur;
            cur=curN;
        }
        return head;
    }
    //链表的中间节点
    public ListNode middleNode(ListNode head){
        //方法1
//        int len=size();
//        ListNode cur=head;
//        int count=0;
//        while(count!=len/2){
//            cur=cur.next;
//            count++;
//        }
//        return cur;
        //方法2
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;
    }
    //返回倒数第k个节点
    public int kthToLast(int k){
//        if(k<=0||k>size()){
//            return -1;
//        }
//        int len=size();
//        int count=len-k;
//        ListNode cur=head;
//        while(count!=0){
//            cur=cur.next;
//            count--;
//        }
//        return cur.val;
        if(k<=0||k>size()){
            return -1;
        }
        ListNode fast=head;
        ListNode slow=head;
        int i=0;
        while(i<k-1){
            fast=fast.next;
        }
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow.val;
    }
}
